/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.base.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.config.properties.JeePlusProperites;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.base.service.GxsqBaseService;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.security.util.JWTUtil;
import com.jeeplus.modules.sys.service.UserService;
import com.jeeplus.modules.sys.utils.RSAUtils;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.work.utils.HttpRequestUtil;
import com.jeeplus.modules.work.utils.WebSocketServer;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * 排队操作记录Controller
 * @author azj
 * @version 2024-09-29
 */
@RestController
@RequestMapping(value = "base")
public class GxsqBaseController extends BaseController {
	@Autowired
	private GxsqBaseService gxsqBaseService;
	@Autowired
	private WebSocketServer webSocketServer;

	@Value("${PRIVATE_KEY}")
	private String PRIVATE_KEY;
	@PostMapping("/login")
	@ApiOperation(value = "用户登录")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户名", name = "userName", required = true, paramType = "query", dataType = "string"),
			@ApiImplicitParam(value = "密码", name = "password", required = true, paramType = "query", dataType = "string")})

	public AjaxJson login(@RequestParam("userName") String userName,
						  @RequestParam("password") String password) throws Exception {
		userName = new String(RSAUtils.decryptByPrivateKey(RSAUtils.decryptBASE64(userName), PRIVATE_KEY));
		password = new String(RSAUtils.decryptByPrivateKey(RSAUtils.decryptBASE64(password), PRIVATE_KEY));
		AjaxJson j = new AjaxJson();
		User user = UserUtils.getByLoginName(userName);
		if (user != null && UserService.validatePassword(password, user.getPassword())) {

			if (JeePlusProperites.NO.equals(user.getLoginFlag())){
				j.setSuccess(false);
				j.setMsg("该用户已经被禁止登陆!");
			}else {
				//修改窗口状态
				gxsqBaseService.updateCountStatus("1",user.getId());
				j.setSuccess(true);
				j.put("token", JWTUtil.createAccessToken(userName, user.getPassword()));
				j.put("refreshToken", JWTUtil.createRefreshToken(userName, user.getPassword()));
				j.put("roleList", user.getRoleList());
			}

		} else {
			j.setSuccess(false);
			j.setMsg("用户名或者密码错误!");
		}
		return j;
	}

	/**
	 * toCheckCode
	 *获取用户电子社保信息
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@PostMapping("toCheckCode")
	public AjaxJson toCheckCode(String Qrcode,String tell,String area,String typeid){
		if(StringUtils.isBlank(Qrcode)){
			return AjaxJson.error("电子社保二维码为空");
		}else{
			String result = HttpRequestUtil.toCheckCode(Qrcode);
			System.out.println(result);
			if(StringUtils.isBlank(result)){
				return AjaxJson.error("电子社保二维码有误");
			}else{
				JSONObject jsonObject = JSONObject.fromObject(result);
				try{
					return gxsqBaseService.getCardValue(jsonObject.getString("idcard"),jsonObject.getString("name"),tell,area,typeid);
				}catch (Exception e){
					return AjaxJson.error("电子二维码无效，请刷新");
				}

			}

		}
	}

	/**
	 * getCardQueue
	 *排队取号
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@PostMapping("getCardQueue")
	public AjaxJson getCardQueue(String idcard,String name,String tell,String area,String typeid){
		return gxsqBaseService.getCardValue(idcard,name,tell,area,typeid);
	}

	/**
	 * queueList
	 * 加载窗口初始状态
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@GetMapping("queueList")
	public AjaxJson queueList(){
		return AjaxJson.success().put("data",gxsqBaseService.queueList(UserUtils.getUser().getId()));
	}


	/**
	 * updateCountStatus
	 * 服务更新
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@GetMapping("updateCountStatus")
	public AjaxJson updateCountStatus(String status,String countid,String max){
		//判断当前窗口是否有未办理完的件
		String pd = gxsqBaseService.checkQueue(countid);
		if("2".equals(status) && null != pd  && pd.equals("1")){
			return AjaxJson.error("当前有未办结件！");
		}else{
			if("2".equals(status)){
				//推送评价仪消息
				if(StringUtils.isNotBlank(max)){
					Map<String,String> map = new HashMap <>(2);
					map.put("type","zt");
					webSocketServer.sendMessage(JSONObject.fromObject(map).toString(), WebSocketServer.tokenAndSessionMap.get(max));
				}
			}
			return AjaxJson.success().put("data",gxsqBaseService.updateCountStatus(status,UserUtils.getUser().getId()));
		}
	}

	/**
	 * jhCardQueue
	 *排队叫号
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@GetMapping("jhCardQueue")
	public AjaxJson jhCardQueue(String countid,String winno,String areaid,String max){
		return gxsqBaseService.jhCardQueue(countid,winno,areaid,max);
	}

	/**
	 * chCardQueue
	 * 重呼
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@GetMapping("chCardQueue")
	public AjaxJson chCardQueue(String queueid,String winno,String number){
		return gxsqBaseService.chCardQueue(queueid,winno,number);
	}

	/**
	 * serviceCardQueue
	 * 开始服务
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@GetMapping("serviceCardQueue")
	public AjaxJson serviceCardQueue(String queueid){
		return gxsqBaseService.serviceCardQueue(queueid);
	}

	/**
	 * endCardQueue
	 * 结束服务
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@GetMapping("endCardQueue")
	public AjaxJson endCardQueue(String queueid,String max){
		return gxsqBaseService.endCardQueue(queueid,max);
	}

	/**
	 * cancleCardQueue
	 * 弃号
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@GetMapping("cancleCardQueue")
	public AjaxJson cancleCardQueue(String queueid,String max){
		return gxsqBaseService.cancleCardQueue(queueid,max);
	}

	/**
	 * 获取点位JSON数据。
	 * @return
	 */
	@GetMapping("treeData")
	public AjaxJson treeData() {
		return AjaxJson.success().put("treeData",gxsqBaseService.findAreaList());
	}

	/**
	 * 通过窗口id获取业务。
	 * @return
	 */
	@GetMapping("getTypeName")
	public AjaxJson getTypeName(String countid) {
		return AjaxJson.success().put("data",gxsqBaseService.getTypeName(countid));
	}
}